﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.UI.Xaml.Media.Imaging;

namespace SharPy.GameClasses
{
    //Коллекция изображений
    static class clImages
    {
        public static List<int> idObject = new List<int>();
        public static List<string> sName = new List<string>();
        public static List<string> sPath = new List<string>();
        public static int id = 0;

        public static int addImage(string pName, string pPath)
        {
            idObject.Add(id++);
            sName.Add(pName);
            sPath.Add(pPath.Trim('\"'));
            return 1;
        }

        public async static Task<BitmapImage> getImage(string pName)
        {
            StorageFile imageFile;
            BitmapImage image = new BitmapImage();

            switch (pName)
            {
                case "black":
                    break;

                default:
                    int index = sName.FindIndex(name => (name == pName));

                    if (index == -1)
                        return null;

                    Windows.Storage.StorageFolder folder = SharPy.Parser.clText.gameFolder;
                    string path = sPath[index].Replace('/', '\\');

                    imageFile = await folder.GetFileAsync(path);

                    Windows.Storage.Streams.FileRandomAccessStream stream = (FileRandomAccessStream)await imageFile.OpenAsync(FileAccessMode.Read);

                    image.SetSource(stream);
                    break;
            }
            return image;
        }

        public async static Task<BitmapImage> getImageByPath(string pName)
        {
            StorageFile imageFile;
            BitmapImage image = new BitmapImage();

            Windows.Storage.StorageFolder folder = SharPy.Parser.clText.gameFolder;
            if (folder == null)
                folder = await SharPy.GameSettings.clSettings.getFolder();

            string path = pName;

            imageFile = await folder.GetFileAsync(path.Trim('\"'));

            Windows.Storage.Streams.FileRandomAccessStream stream = (FileRandomAccessStream)await imageFile.OpenAsync(FileAccessMode.Read);
            
            image.SetSource(stream);
            return image;
        }

        public async static Task<int> DeleteOldImages(GamePage page, Windows.UI.Xaml.HorizontalAlignment p = Windows.UI.Xaml.HorizontalAlignment.Center, object newImage = null)
        {
            Windows.UI.Xaml.Controls.Grid gridMain = (Windows.UI.Xaml.Controls.Grid)page.FindName("gridMain");
            
            foreach (var im in gridMain.Children)
            {
                if (im == newImage)
                    ;
                else if (im.GetType() == typeof(Windows.UI.Xaml.Controls.Image))
                {
                    var a = im as Windows.UI.Xaml.Controls.Image;
                    if (a.HorizontalAlignment == p)
                        gridMain.Children.Remove(im);
                }
            }
            return 1;
        }
    }
}
